package edu.pku.sei.modeler.uml.commands.interactiveoverview;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicActivities.ActivityFinalNode;
import edu.pku.sei.modeler.model.uml.BasicActivities.ControlFlow;
import edu.pku.sei.modeler.model.uml.BasicActivities.InitialNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Interaction;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.IntermediateActivities.DecisionNode;
import edu.pku.sei.modeler.model.uml.IntermediateActivities.ForkNode;
import edu.pku.sei.modeler.model.uml.IntermediateActivities.JoinNode;
import edu.pku.sei.modeler.model.uml.IntermediateActivities.MergeNode;
import edu.pku.sei.modeler.model.uml.Kernel.Comment;
import edu.pku.sei.modeler.uml.commands.UMLCommentLinkCreateCommand;
import edu.pku.sei.modeler.uml.commands.UMLConnectionCreateCommand;
import edu.pku.sei.modeler.uml.commands.UMLConnectionCreateCommandFactory;
import edu.pku.sei.modeler.uml.commands.activity.CreateControlFlowCommand;

public class InteractiveOverviewDiagramConnectionCreateCommandFactory extends
		UMLConnectionCreateCommandFactory {
	public static InteractiveOverviewDiagramConnectionCreateCommandFactory INSTANCE = new InteractiveOverviewDiagramConnectionCreateCommandFactory();
	
	private InteractiveOverviewDiagramConnectionCreateCommandFactory() {
		
	}
	@Override
	public UMLConnectionCreateCommand createCommand(MetaLink link,
			MetaNode source) {
		UMLConnectionCreateCommand cmd = new UMLConnectionCreateCommand(link, source);
		MetaModelElement linkModel = link.getModelElement();
		MetaModelElement sourceModel = source.getModelElement();
		if (sourceModel instanceof DecisionNode) {
			if (linkModel instanceof ControlFlow) {
				cmd.setModelCmd(new InteractionOverviewControlFlowCreateCommand(linkModel, sourceModel));
			}
		} else if (sourceModel instanceof ForkNode) {
			if (linkModel instanceof ControlFlow) {
				cmd.setModelCmd(new InteractionOverviewControlFlowCreateCommand(linkModel, sourceModel));
			}
		} else if (sourceModel instanceof InitialNode) {
			if (linkModel instanceof ControlFlow) {
				cmd.setModelCmd(new InteractionOverviewControlFlowCreateCommand(linkModel, sourceModel));
			}
		} else if (sourceModel instanceof JoinNode) {
			if (linkModel instanceof ControlFlow) {
				cmd.setModelCmd(new InteractionOverviewControlFlowCreateCommand(linkModel, sourceModel));
			}
		} else if (sourceModel instanceof MergeNode) {
			if (linkModel instanceof ControlFlow) {
				cmd.setModelCmd(new InteractionOverviewControlFlowCreateCommand(linkModel, sourceModel));
			}
		} else if (sourceModel instanceof Interaction) {
			if (linkModel instanceof ControlFlow)
				cmd.setModelCmd(new InteractionOverviewControlFlowCreateCommand(linkModel, sourceModel));
		} else if (sourceModel instanceof Comment) {
			if (linkModel instanceof Dependency) {
				cmd.setModelCmd(new UMLCommentLinkCreateCommand(link.getModelElement(), source.getModelElement()));
			}
		}
		if (cmd.getModelCmd() != null)
			return cmd;
		else 
			return null;
	}
	@Override
	public UMLConnectionCreateCommand completeCommand(
			UMLConnectionCreateCommand command, MetaNode target) {
		MetaModelElement linkModel = command.getLink().getModelElement();
		MetaModelElement targetModel = target.getModelElement();
		if (targetModel instanceof DecisionNode) {
			if (linkModel instanceof ControlFlow
					|| linkModel instanceof Dependency) {
				command.setTarget(target);
			}
		} else if (targetModel instanceof ForkNode) {
			if (linkModel instanceof ControlFlow
					|| linkModel instanceof Dependency) {
				command.setTarget(target);
			}
		} else if (targetModel instanceof JoinNode) {
			if (linkModel instanceof ControlFlow
					|| linkModel instanceof Dependency) {
				command.setTarget(target);
			}
		} else if (targetModel instanceof MergeNode) {
			if (linkModel instanceof ControlFlow
					|| linkModel instanceof Dependency) {
				command.setTarget(target);
			}
		} else if (targetModel instanceof ActivityFinalNode) {
			if (linkModel instanceof ControlFlow
					|| linkModel instanceof Dependency){
				command.setTarget(target);
			}
		} else if (targetModel instanceof Interaction) {
			if (linkModel instanceof ControlFlow
					|| linkModel instanceof Dependency){
				command.setTarget(target);
			}
		}
		if (command.getTarget() == target)
			return command;
		else 
			return null;
	}
}
